package com.fuping;

import net.sourceforge.pinyin4j.PinyinHelper;  
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;  
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;  
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;  
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;  
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;  
public class NameChangeMethod {

	/**
	 * 根据名字转换拼音 例如：王大锤转换为wangdachui
	 * @param src 名字
	 * @return 名字的拼音
	 */
	public static String getPingYin(String src) {  
		  
		char[] t1 = null;  
        t1 = src.toCharArray();  
        String[] t2 = new String[t1.length];  
        HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();  
          
        t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);  
        t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);  
        t3.setVCharType(HanyuPinyinVCharType.WITH_V);  
        StringBuffer sb = new StringBuffer();
        int t0 = t1.length;  
        try {  
            for (int i = 0; i < t0; i++) {  
                // 判断是否为汉字字符  
                if (java.lang.Character.toString(t1[i]).matches(  
                        "[\\u4E00-\\u9FA5]+")) {  
                    t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);  
                    sb.append(t2[0]);
                } else{
                	sb.append(java.lang.Character.toString(t1[i]));
                }
            }  
            return sb.toString();  
        } catch (BadHanyuPinyinOutputFormatCombination e1) {  
            e1.printStackTrace();  
        }  
        return sb.toString();   
    }  
  
    /**
     * 根据名字转换名字拼音的首字母 例如：王大锤转换为wdc
     * @param str 
     * @return 名字拼音的首字母
     */
    public static String getPinYinHeadChar(String str) {  
    	  
        StringBuffer sb = new StringBuffer();
        for (int j = 0; j < str.length(); j++) {  
            char word = str.charAt(j); 
            String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);  
            if (pinyinArray != null) { 
                sb.append(pinyinArray[0].charAt(0));
            } else {
                sb.append(word);
            }  
        }  
        return sb.toString();  
    }
  
    // 将字符串转移为ASCII码  
    public static String getCnASCII(String cnStr) {  
        StringBuffer strBuf = new StringBuffer();  
        byte[] bGBK = cnStr.getBytes();  
        for (int i = 0; i < bGBK.length; i++) {  
            strBuf.append(Integer.toHexString(bGBK[i] & 0xff));  
        }  
        return strBuf.toString();  
    }  
    /**
     * 王大锤转换为wangdc
     * @param oldName
     * @return 名字的姓的全拼和名的首字母 
     */
    public static String getNameChange1(String oldName){
    	StringBuffer sb = new StringBuffer();
    	sb.append(getPingYin(oldName.substring(0,1)));
    	sb.append(getPinYinHeadChar(oldName.substring(1)));
    	return sb.toString();
    }
    
    /**
     * 王大锤转换为dc.wang
     * @param oldName
     * @return 名字的名的首字母和姓的全拼，中间一个.
     */
    public static String getNameChange2(String oldName){
    	StringBuffer sb = new StringBuffer();
    	sb.append(getPinYinHeadChar(oldName.substring(1)));
    	sb.append(".");
    	sb.append(getPingYin(oldName.substring(0,1)));
    	
    	return sb.toString();
    }
    /**
     * 王大锤转换为dachui.wang
     * @param oldName
     * @return 名字的名的全拼和姓的全拼，中间一个.
     */
    public static String getNameChange3(String oldName){
    	StringBuffer sb = new StringBuffer();
    	sb.append(getPingYin(oldName.substring(1)));
    	sb.append(".");
    	sb.append(getPingYin(oldName.substring(0,1)));
    	
    	return sb.toString();
    }
  
     
}
